package org.lyl.taobao.dao.impl;

import org.lyl.taobao.api.entity.User;
import org.lyl.taobao.api.utils.DruidConnectionUtils;
import org.lyl.taobao.dao.IUserDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Re
 * @Date: 2021/3/26 14:15
 */
public class UserDaoImpl implements IUserDao {
    DruidConnectionUtils druidConnectionUtils=new DruidConnectionUtils();
    @Override
    public boolean insertUser(Connection connection, User user) {
        String sql = "insert into user (userName,userPwd,balance,userGrade) values(?,?,?,?);";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setString(2, user.getUserPassword());
            preparedStatement.setFloat(3,user.getBalance());
            preparedStatement.setString(4,user.getUserGrade());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            System.out.println("用户添加失败");
            e.printStackTrace();
            return false;
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement, null);
            return true;
        }
    }

    @Override
    public User registerUser(Connection connection, User user) {
        String sql = "select * from `user` where username = ? and userPwd = ? and userGrade = ?;";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user1 = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setString(2, user.getUserPassword());
            preparedStatement.setString(3, user.getUserGrade());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                Integer userId = resultSet.getInt("userId");
                String imgPath = resultSet.getString("img");
                String userName = resultSet.getString("userName");
                String userPassword = resultSet.getString("userPwd");
                float balance = resultSet.getFloat("balance");
                String userGrade = resultSet.getString("userGrade");
                System.out.println( "dao--->imgPath"+imgPath);
                System.out.println( "dao--->Id"+userId);
                System.out.println( "dao--->balance"+balance);
                System.out.println( "dao--->userPassword"+userPassword);
                user1 = new User(userId,imgPath,userName,userPassword,balance,userGrade);
            }
            return user1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
    }

    @Override
    public List<User> checkAllUser(Connection connection) {
        List<User> userList = new ArrayList<>();
        String sql = "select * from user where userGrade = ?;";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"1");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int userId = resultSet.getInt("userId");
                String userName = resultSet.getString("userName");
                String userPwd = resultSet.getString("userPwd");
                int balance = resultSet.getInt("balance");
                String userGrade = resultSet.getString("userGrade");
                userList.add(new User(userId,userName,userPwd,balance,userGrade));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return userList;
    }

    @Override
    public List<User> checkUser(Connection connection, String userName) {
        List<User> userList = new ArrayList<>();
        String sql = "select * from user where userName = ?;";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,userName);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                int userId = resultSet.getInt("userId");
                String name = resultSet.getString("userName");
                String userPwd = resultSet.getString("userPwd");
                int balance = resultSet.getInt("balance");
                String userGrade = resultSet.getString("userGrade");
                userList.add(new User(userId,name,userPwd,balance,userGrade));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return userList;
    }

    @Override
    public boolean deleteUser(Connection connection,String userName) {
        boolean flag = false;
        String sql = "delete from user where userName = ? and userGrade = ?;";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,userName);
            preparedStatement.setString(2,"1");
            int count = preparedStatement.executeUpdate();
            if (count == 1) {
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return flag;
    }

    @Override
    public boolean updateUser(Connection connection,User user) {
        boolean flag = false;
        String sql = "update user set userPwd = ? where userName = ? and userGrade = ? ;";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUserPassword());
            preparedStatement.setString(2,user.getUserName());
            preparedStatement.setString(3, user.getUserGrade());
            if (preparedStatement.executeUpdate() == 1){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return flag;
    }

    @Override
    public Integer getUserIdByC_Id(Connection connection, Integer c_Id) {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select c_UserId from commodity where c_Id = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,c_Id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                Integer userId = resultSet.getInt("c_UserId");
                return userId;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return null;
    }

    @Override
    public float getUserBalanceByUserId(Connection connection, Integer userId) {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select balance from user where userId = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,userId);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                float balance = resultSet.getFloat("balance");
                return balance;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return 0;
    }

    @Override
    public boolean reduceBalanceByUserId(Connection connection,float price,Integer userId) {
        PreparedStatement preparedStatement = null;
        String sql = "update user set balance = balance - "+ price +" where userId = ?;" ;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,userId);
            if (preparedStatement.executeUpdate() == 1){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return false;
    }

    @Override
    public boolean addBalanceByUserId(Connection connection,float price,Integer userId) {
        PreparedStatement preparedStatement = null;
        String sql = "update user set balance = balance + "+ price +" where userId = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,userId);
            if (preparedStatement.executeUpdate() == 1){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return false;
    }

    @Override
    public boolean addPhoto(Connection connection, String imgPath,User user) {
        PreparedStatement preparedStatement = null;
        String sql = "update `user` set img = ? where userId = ?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,imgPath);
            preparedStatement.setInt(2,user.getUserId());
            if (preparedStatement.executeUpdate() == 1){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }
}
